/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mapper;

import java.sql.*;
import java.util.*;

/**
 *
 * @author Quang Thao
 */
public class BookMapper {
    DBMapper map;
    
    public BookMapper() throws SQLException
    {
        map = new DBMapper();
    }
    
    public List GetAll(String type) throws Exception
    {
        List list = new ArrayList<Bean.BookBean>();
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Book WHERE Type='" + type + "'");
        if (rs != null)
        {
            while (rs.next())
            {
                Bean.BookBean book = new Bean.BookBean();
                book.setISBN(rs.getString(1));
                book.setName(rs.getString(2));
                // Type
                BO.Book_TypeBO btBO = new BO.Book_TypeBO();
                Bean.Book_TypeBean bt = new Bean.Book_TypeBean();
                bt = btBO.GetInfo(rs.getString(3));
                book.setType(bt);
                
                book.setDescribe(rs.getString(4));
                book.setPublisher(rs.getString(5));
                int year = Integer.parseInt(rs.getString(6));
                book.setYear(year);
                double cost = Double.parseDouble(rs.getString(7));
                book.setCost(cost);
                int quantity = Integer.parseInt(rs.getString(8));
                book.setQuantity(quantity);
                book.setAuthor(rs.getString(9));
                book.setPicture(rs.getString(10));
                list.add(book);
            }
        }
        return list;
    }
    
    public Bean.BookBean GetInfo(String isbn) throws Exception
    {
        Bean.BookBean book = null;
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Book WHERE ISBN='" + isbn + "'");
        if (rs != null && rs.next())
        {
            book = new Bean.BookBean();
            book.setISBN(rs.getString(1));
            book.setName(rs.getString(2));
            // Type
            BO.Book_TypeBO btBO = new BO.Book_TypeBO();
            Bean.Book_TypeBean bt = new Bean.Book_TypeBean();
            bt = btBO.GetInfo(rs.getString(3));
            book.setType(bt);

            book.setDescribe(rs.getString(4));
            book.setPublisher(rs.getString(5));
            int year = Integer.parseInt(rs.getString(6));
            book.setYear(year);
            double cost = Double.parseDouble(rs.getString(7));
            book.setCost(cost);
            int quantity = Integer.parseInt(rs.getString(8));
            book.setQuantity(quantity);
            book.setAuthor(rs.getString(9));
            book.setPicture(rs.getString(10));
        }
        return book;
    }
    
    public List GetListBySpecial(String special) throws Exception
    {
        List list = new ArrayList<Bean.BookBean>();
        ResultSet rs = map.ExecuteQuery("SELECT b.ISBN,b.BookName,b.Type,b.Describe,b.Publisher,b.YearPublish"
                + ",b.Cost,b.Quantity,b.Author,b.Picture "
                + "FROM Book b, Book_Special bp, Book_State bs "
                + "WHERE b.ISBN=bp.ISBN AND bp.State=bs.ID AND bs.ID='" + special + "'");
        if (rs != null)
        {
            while (rs.next())
            {
                Bean.BookBean book = new Bean.BookBean();
                book.setISBN(rs.getString(1));
                book.setName(rs.getString(2));
                // Type
                BO.Book_TypeBO btBO = new BO.Book_TypeBO();
                Bean.Book_TypeBean bt = new Bean.Book_TypeBean();
                bt = btBO.GetInfo(rs.getString(3));
                book.setType(bt);
                
                book.setDescribe(rs.getString(4));
                book.setPublisher(rs.getString(5));
                int year = Integer.parseInt(rs.getString(6));
                book.setYear(year);
                double cost = Double.parseDouble(rs.getString(7));
                book.setCost(cost);
                int quantity = Integer.parseInt(rs.getString(8));
                book.setQuantity(quantity);
                book.setAuthor(rs.getString(9));
                book.setPicture(rs.getString(10));
                list.add(book);
            }
        }
        return list;
    }
}
